package com.lys.msmservice.service.impl;


import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;

import com.aliyuncs.profile.DefaultProfile;
import com.lys.msmservice.service.MsmService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Map;

@Service
@Slf4j
public class MsmServiceImpl implements MsmService {
    @Override
    public boolean send(Map<String, Object> parms, String phone) {
        if (StringUtils.isEmpty(phone)) {
            return false;
        }

        DefaultProfile profile =
                DefaultProfile.getProfile("cn-hangzhou", "LTAI5tBFDgFUHxV3EHfRmUcY", "003V7MBReTdUD1Q0UMGHuza9C8HNAq");

        //设置相关固定的参数
        IAcsClient client = new DefaultAcsClient(profile);

        SendSmsRequest request = new SendSmsRequest();
        request.setSignName("阿里云短信测试");
        request.setTemplateCode("SMS_154950909");
        request.setPhoneNumbers(phone);
        request.setTemplateParam(JSONObject.toJSONString(parms));

        try {
            SendSmsResponse response = client.getAcsResponse(request);
            return true;

        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}
